#include "TM1637m1.h"
#include <EEPROM.h>
bool En_63006074_2;
int Value_63006074_2;
byte Dot_63006074_2;
byte Brightness_63006074_2;
TM1637 TM1637_63006074_2(3, 2);
byte ValDisp_63006074_2[] = {48,48,48,48};
String temp_63006074_2;
int _swi3;
bool _gen1I = 0;
bool _gen1O = 0;
unsigned long _gen1P = 0UL;
bool _bounseInputD10S = 0;
bool _bounseInputD10O = 0;
unsigned long _bounseInputD10P = 0UL;
byte _swi1;
bool _pzs2OES = 0;
int _swi4;
bool _bounseInputD6S = 0;
bool _bounseInputD6O = 0;
unsigned long _bounseInputD6P = 0UL;
bool _gen3I = 0;
bool _gen3O = 0;
unsigned long _gen3P = 0UL;
bool _trgr2 = 0;
bool _bounseInputD11S = 0;
bool _bounseInputD11O = 0;
unsigned long _bounseInputD11P = 0UL;
bool _gen2I = 0;
bool _gen2O = 0;
unsigned long _gen2P = 0UL;
byte _swi5;
bool _tim1I = 0;
bool _tim1O = 0;
unsigned long _tim1P = 0UL;
bool _SEEPR1OSN = 0;
void setup()
{
    pinMode(11, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(5, OUTPUT);
    digitalWrite(5, 0);
    pinMode(7, OUTPUT);
    digitalWrite(7, 0);
    _bounseInputD10O =  digitalRead(10);
    _bounseInputD11O =  digitalRead(11);
    _bounseInputD6O =  digitalRead(6);
    Dot_63006074_2=0;
    Brightness_63006074_2=7;
    TM1637_63006074_2.display(ValDisp_63006074_2 , Dot_63006074_2 , Brightness_63006074_2);
}
void loop()
{
    bool  _bounceInputTmpD10 =  (digitalRead (10));
    if (_bounseInputD10S)
    {
        if (millis() >= (_bounseInputD10P + 40))
        {
            _bounseInputD10O= _bounceInputTmpD10;
            _bounseInputD10S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD10 != _bounseInputD10O)
        {
            _bounseInputD10S=1;
            _bounseInputD10P = millis();
        }
    }
    bool  _bounceInputTmpD6 =  (digitalRead (6));
    if (_bounseInputD6S)
    {
        if (millis() >= (_bounseInputD6P + 40))
        {
            _bounseInputD6O= _bounceInputTmpD6;
            _bounseInputD6S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD6 != _bounseInputD6O)
        {
            _bounseInputD6S=1;
            _bounseInputD6P = millis();
        }
    }
    bool  _bounceInputTmpD11 =  (digitalRead (11));
    if (_bounseInputD11S)
    {
        if (millis() >= (_bounseInputD11P + 40))
        {
            _bounseInputD11O= _bounceInputTmpD11;
            _bounseInputD11S=0;
        }
    }
    else
    {
        if (_bounceInputTmpD11 != _bounseInputD11O)
        {
            _bounseInputD11S=1;
            _bounseInputD11P = millis();
        }
    }
    //Плата:1
    if(((((((analogRead (1))) <= (20)) && (!(((analogRead (0))) >= (20))))) || (_gen3O)))
    {
        if((!_pzs2OES))
        {
             tone(4, 500);
            _pzs2OES =1;
        }
    }
     else 
    {
        if(_pzs2OES)
        {
            noTone(4);
            _pzs2OES =0;
        }
    }
    if(((analogRead (0))) >= (30))
    {
        _swi5=4;
    }
    else
    {
        _swi5=0;
    }
    if(((!(_bounseInputD10O)) || (((analogRead (1))) <= (1015))))
    {
        _swi1=7;
    }
    else
    {
        _swi1=0;
    }
    if(((analogRead (0))) >= (30))
    {
        _swi4= (analogRead (0));
    }
    else
    {
        _swi4=((analogRead (1)))/(1);
    }
    if(!(_bounseInputD10O))
    {
        _swi3=(readIntegerFromEEPROM(0, 0, 0x0));
    }
    else
    {
        _swi3=_swi4;
    }
    En_63006074_2 = 1;
    Value_63006074_2 = _swi3;
    Dot_63006074_2 = _swi5;
    Brightness_63006074_2 = _swi1;
    // TM1637m1
    if (En_63006074_2)
    {
        	if (Value_63006074_2 > 0)
        	
        {
            	temp_63006074_2 = String(Value_63006074_2);
            	ValDisp_63006074_2[0]=(temp_63006074_2.charAt(0))-0x30;
            	
        }
        	else
        	
        {
            	ValDisp_63006074_2[0]=62;
            	abs(Value_63006074_2);
            	temp_63006074_2 = String(Value_63006074_2);
            	
        }
        	ValDisp_63006074_2[1]=(temp_63006074_2.charAt(1))-0x30;
        	ValDisp_63006074_2[2]=(temp_63006074_2.charAt(2))-0x30;
        	ValDisp_63006074_2[3]=(temp_63006074_2.charAt(3))-0x30;
        	TM1637_63006074_2.display(ValDisp_63006074_2 , Dot_63006074_2 , Brightness_63006074_2);
    }
    //Плата:2
//Наименование:ЗАПИСЬ В ПАМЯТЬ
    if (!(_bounseInputD11O))
    {
         if (_tim1I) 
        {
             if (_isTimer(_tim1P, 500)) 
            {
                _tim1O = 1;
            }
        }
         else 
        {
            _tim1I =1;
            _tim1P = millis();
        }
    }
     else 
    {
        _tim1O = 0;
        _tim1I = 0;
    }
    if (_tim1O) 
    {
        if (! _gen3I)
        {
            _gen3I = 1;
            _gen3O = 1;
            _gen3P = millis();
        }
    }
     else 
    {
        _gen3I = 0 ;
        _gen3O= 0;
    }
     if (_gen3I &&  _gen3O)   _gen3O = !(_isTimer(_gen3P , 500));
    if(!(_bounseInputD11O))
    {
         if(!_SEEPR1OSN)
        {
            (updateIntegerToEEPROM(0, 0, 0x0, (_swi4)));
            _SEEPR1OSN=1;
        }
    }
    else
    {
         if(_SEEPR1OSN)
        {
            _SEEPR1OSN=0;
        }
    }
    //Плата:3
//Наименование:УПР. ПИТАНИЕМ
    if (((analogRead (3))) <= (560)) 
    {
         if (! _gen2I) 
        {
            _gen2I = 1;
            _gen2O = 1;
            _gen2P = millis();
        }
    }
     else 
    {
        _gen2I = 0 ;
        _gen2O= 0;
    }
    if (_gen2I) 
    {
          if (_isTimer (_gen2P , 500)) 
        {
             _gen2P = millis();
            _gen2O = ! _gen2O;
        }
    }
    digitalWrite(5, _gen2O);
    if (((analogRead (3))) >= (400)) 
    {
        if (! _gen1I)
        {
            _gen1I = 1;
            _gen1O = 1;
            _gen1P = millis();
        }
    }
     else 
    {
        _gen1I = 0 ;
        _gen1O= 0;
    }
     if (_gen1I &&  _gen1O)   _gen1O = !(_isTimer(_gen1P , 1000));
    if(!(_bounseInputD6O)) _trgr2 = 0;
    if(_gen1O) _trgr2 = 1;
    digitalWrite(7, _trgr2);
}
bool _isTimer(unsigned long startTime, unsigned long period)
{
    unsigned long currentTime;
    currentTime = millis();
    if (currentTime>= startTime) 
    {
        return (currentTime>=(startTime + period));
    }
     else 
    {
        return (currentTime >=(4294967295-startTime+period));
    }
}
byte readByteFromEEPROM(int addres, byte bitAddres, byte chipAddres)
{
    return EEPROM.read(addres);
}
void updateByteToEEPROM(int addres, byte bitAddres, byte chipAddres, byte value)
{
    return EEPROM.update(addres, value);
}
int readIntegerFromEEPROM(int addres, byte bitAddres, byte chipAddres)
{
    byte hight = readByteFromEEPROM(addres,  bitAddres,  chipAddres);
    byte low = readByteFromEEPROM((addres+1),  bitAddres,  chipAddres);
    return  word(hight, low);
}
void updateIntegerToEEPROM(int addres, byte bitAddres, byte chipAddres, int value)
{
    updateByteToEEPROM(addres,  bitAddres,  chipAddres, highByte(value));
    updateByteToEEPROM((addres+1),  bitAddres,  chipAddres, lowByte(value));
}
